<?php
/*******************************************************************************
 *
 *  Name: Report Model
 *
 *  Date: 4/1/2012
 *  
 *  Description: This model allows for access to the Report table in our 
 *               database.
 *  
 *  Author: 
 *  
 *  Todo:
 *     NONE
 *
 *  Modification History:  
 * 
 *   Date         Name
 *  ----------   --------------
 *  2012-04-26   HAYDEN WAISANEN
 *  2012-04-25   HAYDEN WAISANEN
 *  2012-04-23   DAVID JARMAN
 *  2012-04-23   TRAVIS ROUS
 *  2012-04-22   TRAVIS ROUS
 *  2012-04-22   HAYDEN WAISANEN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-09   HAYDEN WAISANEN
 * 
 *****************************************************************************/
?>
<?php
class Report_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->database();
    }
    
    function get($ReportID)
    {
        $query = $this->db->get_where('Report', array('ReportID' => $ReportID));
        return $query->result_array();
    }
    function getAll($where = '')
    {
       //$query = $this->db->query('SELECT * FROM Report ORDER BY ReportTime DESC');
       $query = $this->db->query("

       SELECT *,
CASE (If(( SELECT Status FROM Incident WHERE Report.IncidentID =  Incident.IncidentID) IS NULL, 0, 
( SELECT Status FROM Incident WHERE Report.IncidentID =  Incident.IncidentID)+1))
WHEN 0 THEN 'Awaiting Review'
WHEN 1 THEN 'Awaiting Assignment'
WHEN 2 THEN 'Awaiting Repair'
WHEN 3 THEN 'Under Repair'
WHEN 4 THEN 'Resolved'
END
AS Status
FROM Report ".$where);
       return $query->result_array();
    }

    function getWithIncidentID($IncidentID)
    {
        $query = $this->db->get_where('Report', array('IncidentID' => $IncidentID));
        return $query->result_array();
    }

    function getAwaitingAnalysis()
    {
       $query = $this->db->query('SELECT * FROM Report WHERE IncidentID IS null ORDER BY ReportTime DESC');
       return $query->result_array();
    }

    function getWithCustomerInfo($where = 'WHERE 1')
    {
       //$query = $this->db->query('SELECT Report.ReportID AS ReportID_save, Report.*, CustomerInfo.* FROM Report LEFT OUTER JOIN CustomerInfo ON Report.ReportID = CustomerInfo.ReportID '.$where);
       $query = $this->db->query("

       SELECT Report.ReportID AS ReportID_save, Report.*, CustomerInfo.*,
CASE (If(( SELECT Status FROM Incident WHERE Report.IncidentID =  Incident.IncidentID) IS NULL, 0, 
( SELECT Status FROM Incident WHERE Report.IncidentID =  Incident.IncidentID)+1))
WHEN 0 THEN 'Awaiting Review'
WHEN 1 THEN 'Awaiting Assignment'
WHEN 2 THEN 'Awaiting Repair'
WHEN 3 THEN 'Under Repair'
WHEN 4 THEN 'Resolved'
END
AS Status
FROM Report LEFT OUTER JOIN CustomerInfo ON Report.ReportID = CustomerInfo.ReportID ".$where);

       return $query->result_array();
    }
	function findID($time)
	{
		$query = $this->db->get('Report');

		foreach ($query->result() as $row)
		{
			//echo $row->ReportTime;
			
			if ($row->ReportTime == $time)
				return $row->ReportID;
			
		}
	}
	
    function insert($fields)
    {

        $this->db->insert('Report', $fields);
        return true;
    }
	function insert_CustomerInfo($fields)
    {
		//$query = $this->db->get_where('CustomerInfo', array('ReportID' => $fields['ReportID']));
        //if( $query->num_rows() > 0 )
        //   return false;
	
        $this->db->insert('CustomerInfo', $fields);
        return true;
    }

    function update($fields)
    {
        $this->db->update('Report', $fields, array('ReportID' => $fields['ReportID']));
    }
    function delete($fields)
    {
         $this->db->where('ReportID', $fields['ReportID']);
         $this->db->delete('Report'); 
    }
}
?>
